package net.awpspace.mp3player.screen;

import java.io.IOException;
import java.util.Vector;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemStateListener;
import javax.microedition.lcdui.List;
import javax.microedition.lcdui.StringItem;

import net.awpspace.mp3player.Application;
import net.awpspace.mp3player.entity.Mp3File;
import net.awpspace.mp3player.utils.AudioManager;

public class ListMusic extends List implements ItemStateListener,
		CommandListener {

	private Vector list;
	// private Command musicCommand;
	private Command details;
	private Command delete;
	private Command addFavorite;
	private Command back;
	private Command select;
	private Image icon;

	public ListMusic(String arg0, int arg1) {
		super(arg0, arg1);
		try {
			icon = Image.createImage("/play_music.png");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		createCommand();
	}

	public void createCommand() {
		details = new Command("Details", Command.SCREEN, 0);
		delete = new Command("Delete", Command.SCREEN, 0);
		addFavorite = new Command("Add Favorite", Command.SCREEN, 0);
		back = new Command("Back", Command.BACK, 0);

		select = new Command("Select", Command.ITEM, 0);
		this.addCommand(details);
		this.addCommand(delete);
		this.addCommand(addFavorite);
		this.addCommand(back);
		this.setCommandListener(this);
		this.setSelectCommand(select);
	}

	public void createListMusic(Vector list) {
		this.list = list;
		for (int i = 0; i < this.list.size(); i++) {
			this.append(((Mp3File) this.list.elementAt(i)).getFile_name(), icon);
		}
	}

	public void itemStateChanged(Item arg0) {
		// TODO Auto-generated method stub
		select();
	}

	public void delete() {

	}

	public void select() {
		int index = this.getSelectedIndex();
		Mp3File mp3 = (Mp3File) list.elementAt(index);
		AudioManager.getInstance().stopAll();
		PlayScreen.getInstance().setCurrentFile(mp3);
		PlayScreen.getInstance().playCurrent();
		PlayScreen.setStateButtonPlay(1);
		Application.setTab(0);
		PlayScreen.isPlaying = true;
		Application.getDisplay().setCurrent(PlayScreen.getInstance());
		System.out.println(index + ":" + mp3.getFile_name());
	}

	public void commandAction(Command cm, Displayable dis) {
		// TODO Auto-generated method stub
		if (cm.getLabel().equals("Back")) {
		} else if (cm.getLabel().equals("Details")) {
			System.out.println("Details");
		} else if (cm.getLabel().equals("Add Favorite")) {
			System.out.println("Favorite");
		} else if (cm.getLabel().equals("Delete")) {
			System.out.println("Delete");
			delete();
		} else if (cm.getLabel().equals("Select")) {
			System.out.println("Delete");
			select();
		}

	}

}
